package com.henghai.zhenxuan.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;


import com.henghai.zhenxuan.R;

import java.util.ArrayList;
import java.util.List;

public class VerticalStepView extends LinearLayout {
    private List<StepModel> mData = new ArrayList<>();
    private Context mContext;

    public VerticalStepView(Context context) {
        this(context, null);
        init();
    }

    public VerticalStepView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init();
    }

    public VerticalStepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();

    }

    private void init() {
        setOrientation(VERTICAL);
        mData = getData();//获取数据
        for (int i = 0; i < mData.size(); i++) {
            //获取布局，注意第二个参数一定是ViewGroup，否则margin padding之类的属性将不能使用
            View itemview = LayoutInflater.from(mContext).inflate(com.henghai.zhenxuan.R.layout.item_stepview, this, false);
            TextView description = (TextView) itemview.findViewById(R.id.description_tv);
            TextView time_tv = (TextView) itemview.findViewById(R.id.time_tv);

            View line = itemview.findViewById(R.id.line_v);
            AppCompatImageView icon = (AppCompatImageView) itemview.findViewById(R.id.step_icon_center_iv);
            description.setText(mData.get(i).getDescription());
            time_tv.setText(mData.get(i).getTime());
            //根据不同状态设置不同图标
            switch (mData.get(i).getCurrentState()) {
                case StepModel.STATE_COMPLETED:
                    icon.setImageResource(R.drawable.shape_step_center);
                    break;
                case StepModel.STATE_DEFAULT:
                    //结尾图标隐藏竖线
                    line.setVisibility(GONE);
                    icon.setImageResource(R.drawable.shape_step_center);
                    break;
                case StepModel.STATE_PROCESSING:
                    icon.setImageResource(R.drawable.shape_step_processing);
                    break;
            }
            this.addView(itemview);
        }
        requestLayout();//重新绘制布局
        invalidate();//刷新当前界面
    }

    public List<StepModel> getData() {
        return mData;
    }

    public void setData(List<StepModel> mData) {
        this.mData = mData;

        init();
    }
}